package com.zhiwei.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiwei.mapper.TraderMapper;
import com.zhiwei.pojo.Trader;
import com.zhiwei.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TraderServiceImpl implements TraderService{
    @Autowired
    private TraderMapper traderMapper;

    @Override
    public Trader getTrader(Integer traderId) {
        return traderMapper.selectById(traderId);
    }

    @Override
    public List<Trader> getTraderLists() {
        List<Trader> traders = traderMapper.selectList(null);
        return traders;
    }

    @Override
    public PageResult getTraders(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<Trader> qw = new QueryWrapper<>();
        qw.like(StringUtils.hasLength(pageResult.getQuery()),"name", pageResult.getQuery())
                .eq("type", pageResult.getStart())
                .eq("delete_flag",false)
                .eq("isystem",false);
        IPage iPage = traderMapper.selectPage(page, qw);

        List<Trader> requests = iPage.getRecords();
        PageResult Result = pageResult.setRows(requests).setTotal(iPage.getTotal());
        return Result;
    }

    @Override
    public void updateTrader(Trader trader) {
        traderMapper.updateById(trader);
    }

    @Override
    public void deleteTrader(Integer id) {
        Trader trader = traderMapper.selectById(id);
        trader.setDeleteFlag(true);
        traderMapper.updateById(trader);
    }

    @Override
    public void insertTrader(Trader trader) {
        trader.setDeleteFlag(false)
        .setIsystem(false)
        .setStatus(false);
        traderMapper.insert(trader);
    }

}
